package com.mmall.controller.backend;

import com.google.common.collect.Maps;
import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.Product;
import com.mmall.pojo.User;
import com.mmall.service.IFileService;
import com.mmall.service.IProductService;
import com.mmall.service.IUserService;
import com.mmall.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/manage/product")
public class ProductManagerController {
    @Autowired
    IUserService iUserService;
    @Autowired
    IProductService iProductService;
    @Autowired
    IFileService iFileService;

    @RequestMapping("save.do")
    @ResponseBody
    public ServerResponse productSave(HttpServletRequest request, Product product) {
       /* String loginToken = CookieUtil.readLoginToken(request);
        if(StringUtils.isEmpty(loginToken)){
            return ServerResponse.createByErrorMessage("用户未登录，无法获取当前用户信息");
        }
        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.string20bj(userJsonStr,User.class);
        if(user == null ){
            return ServerResponse.createByErrorMessage("用户未登录，请先登陆");
        }
        if (iUserService.checkAdminRole(user).isSuccess()){
            //增加产品逻辑
           return iProductService.saveOrUpdateProduct(product);
        }else {
            return ServerResponse.createByErrorMessage("无权限操作");
        }*/

        //全部通过拦截器验证是否登陆以及权限
        return iProductService.saveOrUpdateProduct(product);
    }


    @RequestMapping("set_sale_status.do")
    @ResponseBody
    public ServerResponse setSaleStatus(HttpServletRequest request, Integer productId, Integer status) {
       /* String loginToken = CookieUtil.readLoginToken(request);
        if(StringUtils.isEmpty(loginToken)){
            return ServerResponse.createByErrorMessage("用户未登录，无法获取当前用户信息");
        }
        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.string20bj(userJsonStr,User.class);
        if(user == null ){
            return ServerResponse.createByErrorMessage("用户未登录，请先登陆");
        }
        if (iUserService.checkAdminRole(user).isSuccess()){
            //增加产品逻辑
          return   iProductService.setSaleStatus(productId,status);
        }else {
            return ServerResponse.createByErrorMessage("无权限操作");
        }*/

        //全部通过拦截器验证是否登陆以及权限
        return iProductService.setSaleStatus(productId, status);

    }


    @RequestMapping("detail.do")
    @ResponseBody
    public ServerResponse getDetail(HttpServletRequest request, Integer productId) {
        /*String loginToken = CookieUtil.readLoginToken(request);
        if(StringUtils.isEmpty(loginToken)){
            return ServerResponse.createByErrorMessage("用户未登录，无法获取当前用户信息");
        }
        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.string20bj(userJsonStr,User.class);
        if(user == null ){
            return ServerResponse.createByErrorMessage("用户未登录，请先登陆");
        }
        if (iUserService.checkAdminRole(user).isSuccess()){
            //填充业务
            return   iProductService.manageProductDetail(productId);
        }else {
            return ServerResponse.createByErrorMessage("无权限操作");
        }*/

        //全部通过拦截器验证是否登陆以及权限
        return iProductService.manageProductDetail(productId);

    }

    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse getList(HttpServletRequest request, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value =
            "pageSize", defaultValue = "10") int pageSize) {
       /* String loginToken = CookieUtil.readLoginToken(request);
        if(StringUtils.isEmpty(loginToken)){
            return ServerResponse.createByErrorMessage("用户未登录，无法获取当前用户信息");
        }
        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.string20bj(userJsonStr,User.class);
            if (user == null){
                return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆，请登陆管理员");

            }
            if(iUserService.checkAdminRole(user).isSuccess()){
                //填充业务
                return iProductService.getProductList(pageNum,pageSize);
            }else{
                return ServerResponse.createByErrorMessage("无权限操作");
            }*/

        //全部通过拦截器验证是否登陆以及权限
        return iProductService.getProductList(pageNum, pageSize);


    }


    @RequestMapping("productSearch.do")
    @ResponseBody
    public ServerResponse productSearch(HttpServletRequest request, String productName, Integer productId, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value =
            "pageSize", defaultValue = "10") int pageSize) {
       /* String loginToken = CookieUtil.readLoginToken(request);
        if(StringUtils.isEmpty(loginToken)){
            return ServerResponse.createByErrorMessage("用户未登录，无法获取当前用户信息");
        }
        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.string20bj(userJsonStr,User.class);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆，请登陆管理员");

        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            //填充业务
            return iProductService.searchProduct(productName,productId,pageNum,pageSize);
        }else{
            return ServerResponse.createByErrorMessage("无权限操作");
        }*/
        return iProductService.searchProduct(productName, productId, pageNum, pageSize);

        //全部通过拦截器验证是否登陆以及权限

    }

    @RequestMapping("upload.do")
    @ResponseBody
    public ServerResponse upload(MultipartFile file, HttpServletRequest request) {
  /*      String loginToken = CookieUtil.readLoginToken(request);
        if(StringUtils.isEmpty(loginToken)){
            return ServerResponse.createByErrorMessage("用户未登录，无法获取当前用户信息");
        }
        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.string20bj(userJsonStr,User.class);
            if (user == null){
                return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆，请登陆管理员");

            }
        if(iUserService.checkAdminRole(user).isSuccess()){
            //填充业务
            String path = request.getSession().getServletContext().getRealPath("upload");
            String targetFile = iFileService.upload(file,path);
            String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFile;

            Map fileMap = Maps.newHashMap();
            fileMap.put("uri",targetFile);
            fileMap.put("url",url);
            return ServerResponse.createBySuccess(fileMap);
        }else{
            return ServerResponse.createByErrorMessage("无权限操作");
        }*/

        //全部通过拦截器验证是否登陆以及权限
        String path = request.getSession().getServletContext().getRealPath("upload");
        String targetFile = iFileService.upload(file, path);
        String url = PropertiesUtil.getProperty("ftp.server.http.prefix") + targetFile;
        Map fileMap = Maps.newHashMap();
        fileMap.put("uri", targetFile);
        fileMap.put("url", url);
        return ServerResponse.createBySuccess(fileMap);
    }

    @RequestMapping("richtext_img_upload.do")
    @ResponseBody
    public Map richtextImgUpload(MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = Maps.newHashMap();
       /* String loginToken = CookieUtil.readLoginToken(request);
        if(StringUtils.isEmpty(loginToken)){
            resultMap.put("success",false);
            resultMap.put("msg","用户未登录，无法获取当前用户信息");
            return resultMap;
        }
        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.string20bj(userJsonStr,User.class);
        if (user == null){
            resultMap.put("success",false);
            resultMap.put("msg","请登陆管理员");
            return resultMap;

        }
        //f富文本中对于返回值有自己的要求，我们使用simditor所以按照simditor的要求进行返回

        if(iUserService.checkAdminRole(user).isSuccess()){
            //填充业务
            String path = request.getSession().getServletContext().getRealPath("upload");
            String targetFile = iFileService.upload(file,path);
            if(StringUtils.isBlank(targetFile)){
                resultMap.put("success",false);
                resultMap.put("msg","上传失败");
                return  resultMap;
            }
            String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFile;

            Map fileMap = Maps.newHashMap();
            fileMap.put("uri",targetFile);
            fileMap.put("url","上传成功");
            fileMap.put("file",url);

            response.addHeader("Access-Control-Allow-Headers","X-File-Name");
            return resultMap;
        }else{
            resultMap.put("success",false);
            resultMap.put("msg","无权限操作");
            return  resultMap;
        }
    }*/
        //全部通过拦截器验证是否登陆以及权限
        String path = request.getSession().getServletContext().getRealPath("upload");
        String targetFile = iFileService.upload(file, path);
        if (StringUtils.isBlank(targetFile)) {
            resultMap.put("success", false);
            resultMap.put("msg", "上传失败");
            return resultMap;
        }
        String url = PropertiesUtil.getProperty("ftp.server.http.prefix") + targetFile;

        Map fileMap = Maps.newHashMap();
        fileMap.put("uri", targetFile);
        fileMap.put("url", "上传成功");
        fileMap.put("file", url);

        response.addHeader("Access-Control-Allow-Headers", "X-File-Name");
        return resultMap;

    }
}
